package com.iweb.goods.dao;

import com.iweb.goods.model.Goods;
import com.iweb.util.JdbcUtil;
import com.iweb.util.PageModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsDao {
    @Override
    public long findByGoodsName(String goodsName) {
        String sql = "select count(*) cou from tb_goods where goodsname = ?";
        List<Map<String, Object>> list = JdbcUtil.selectBySql(sql, goodsName);
        Map<String, Object> map = list.get(0);
        long res = (Long) map.get("cou");
        return res;
    }

    @Override
    public List<Goods> selectByPage(PageModel pageModel) {
        List<Goods> goodses = new ArrayList<>();
        String likeValue = pageModel.getLikeValue();
        StringBuffer sql = new StringBuffer("select goodsid,userid,goodsname,goodsprice,date_format(createtime,'%Y-%m-%d %H:%i:%s') createtime,description from tb_goods " +
                " where goodsid like ? or goodsname like ? or description like ?");
        int recordCount = JdbcUtil.selectBySql(sql.toString(), "%" + likeValue + "%", "%" + likeValue + "%", "%" + likeValue + "%").size();
        pageModel.setRecordCount(recordCount);//修改pageModel对象的总记录数
        sql.append(" order by goodsid desc limit ? ,?");
        int startNo = pageModel.getPageSize() * (pageModel.getPageNo() - 1);
        List<Map<String, Object>> list = JdbcUtil.selectBySql(sql.toString(), "%" + likeValue + "%", "%" + likeValue + "%", "%" + likeValue + "%", startNo, pageModel.getPageSize());
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            Integer goodsid = (Integer) map.get("goodsid");
            Integer userid = (Integer) map.get("userid");
            String goodsname = (String) map.get("goodsname");
            String goodsprice = (String) map.get("goodsprice");
            String createtime = (String) map.get("createtime");
            String description = (String) map.get("description");
            goodses.add(new Goods(goodsid, userid, goodsname, goodsprice, createtime, description));
        }
        return goodses;
    }

    @Override
    public List<Goods> selectByMyPage(PageModel pageModel, Integer userid) {
        List<Goods> goodses = new ArrayList<>();
        String likeValue = pageModel.getLikeValue();
        StringBuffer sql = new StringBuffer("select goodsid,userid,goodsname,goodsprice,date_format(createtime,'%Y-%m-%d %H:%i:%s') createtime,description from tb_goods " +
                " where userid = ? and (goodsid like ? or goodsname like ? or description like ?)");
        int recordCount = JdbcUtil.selectBySql(sql.toString(), userid, "%" + likeValue + "%", "%" + likeValue + "%", "%" + likeValue + "%").size();
        pageModel.setRecordCount(recordCount);//修改pageModel对象的总记录数
        sql.append(" order by goodsid desc limit ? ,?");
        int startNo = pageModel.getPageSize() * (pageModel.getPageNo() - 1);
        List<Map<String, Object>> list = JdbcUtil.selectBySql(sql.toString(), userid, "%" + likeValue + "%", "%" + likeValue + "%", "%" + likeValue + "%", startNo, pageModel.getPageSize());
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            Integer goodsid = (Integer) map.get("goodsid");
            String goodsname = (String) map.get("goodsname");
            String goodsprice = (String) map.get("goodsprice");
            String createtime = (String) map.get("createtime");
            String description = (String) map.get("description");
            goodses.add(new Goods(goodsid, userid, goodsname, goodsprice, createtime, description));
        }
        return goodses;
    }

    @Override
    public Goods selectById(Integer goodsid) {

        String sql = "select goodsid,userid,goodsname,goodsprice,date_format(createtime,'%Y-%m-%d %H:%i:%s') createtime,description from tb_goods where goodsid=?";
        List<Map<String, Object>> list = JdbcUtil.selectBySql(sql, goodsid);
        Map<String, Object> map = list.get(0);
        Integer userid = (Integer) map.get("userid");
        String goodsname = (String) map.get("goodsname");
        String goodsprice = (String) map.get("goodsprice");
        String createtime = (String) map.get("createtime");
        String description = (String) map.get("description");
        Goods goods = new Goods(goodsid, userid, goodsname, goodsprice, createtime, description);
        return goods;
    }

    @Override
    public int saveOne(Goods goods) {
        String sql = "insert into tb_goods values(null,?,?,?,now(),?)";
        JdbcUtil.editBySql(sql, goods.getUserid(), goods.getGoodsname(), goods.getGoodsprice(), goods.getDescription());
        return 0;
    }

    @Override
    public int updateOne(Goods goods) {
        String sql = "update tb_goods set goodsname=? ,goodsprice=? , description = ? where goodsid= ?";
        return JdbcUtil.editBySql(sql, goods.getGoodsname(), goods.getGoodsprice(), goods.getDescription(), goods.getGoodsid());
    }

    @Override
    public int deleteOne(Integer goodsid) {
        String sql = "delete from tb_goods where goodsid=? ";
        return JdbcUtil.editBySql(sql, goodsid);
    }
}
